package com.jams.goodox.activity.mine.red;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hangzhou.jin.customview.JRecyclerView;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.adapter.RedPacketChild3Adapter;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.entity.MyRedPacket;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.ToastUtil;

import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Jin on 2016/5/14.
 * Description 我的红包
 */
public class MineRedPackets2Fragment extends BaseFragment {

	@Bind(R.id.jRecyclerView)
    JRecyclerView mJRecyclerView;

	private RedPacketChild3Adapter mAdapter;

	public static MineRedPackets2Fragment getInstance(boolean isMinePicket){
		MineRedPackets2Fragment mineRedPackets2Fragment=new MineRedPackets2Fragment();
		Bundle bundle=new Bundle();
		bundle.putBoolean("isMinePicket",isMinePicket);
		mineRedPackets2Fragment.setArguments(bundle);
		return mineRedPackets2Fragment;
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


		View view = inflater.inflate(R.layout.j_recyclerview, container, false);
		ButterKnife.bind(this, view);

		mJRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
		mAdapter = new RedPacketChild3Adapter(null);
		mJRecyclerView.setAdapter(mAdapter);
		mAdapter.openLoadMore(PAGE_SIZE, true);

		// 自定义上拉View
		mAdapter.setLoadingView(getLoadMoreBootView(mContext));

		mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
			@Override
			public void onLoadMoreRequested() {
				loadData(false);
			}
		});

		mJRecyclerView.loading();
		loadData(true);
		mJRecyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				// 刷新代码
				loadData(true);
			}
		});

		return view;
	}

	int curPage = 1;

	private boolean hasData = false;

	private void loadData(final boolean isFirstPage) {
		if (isFirstPage)
			curPage = 1;
		else
			curPage++;
		Map<String,String> params= Constant.getPagerParams(curPage);
		params.put("type","1");
		enqueue(createCallService(MineService.class).getMyRedPacketList(params),new RetrofitCallbackListener<MyRedPacket>() {
			@Override
			public void onSuccess(Call<MyRedPacket> call, MyRedPacket result) {
				mJRecyclerView.loadComplete();
				mJRecyclerView.setRefreshing(false);
				if(result.getData()==null||result.getData().getResultList()==null||result.getData().getPageInfo()==null)return;
				List<MyRedPacket.DataBean.ResultListBean> list = result.getData().getResultList();
				if (null != list) {
					if (isFirstPage) {
						mAdapter.setNewData(list);
						mAdapter.notifyDataChangedAfterLoadMore(list.size() == PAGE_SIZE);
						if (list.size() == 0) {
							mJRecyclerView.loadCompleteNoData(R.mipmap.holder_no_redpacket, MineRedPacketsActivity.NONE_RED);
							return;
						}
						hasData = list.size() > 0;
					} else {
						mAdapter.notifyDataChangedAfterLoadMore(list, list.size() == PAGE_SIZE);
					}
				}
			}

			@Override
			public void onError(String msg) {
				mJRecyclerView.setRefreshing(false);
				if (!hasData)
					mJRecyclerView.loadCompleteNoData(R.mipmap.holder_no_redpacket, MineRedPacketsActivity.NONE_RED);
				ToastUtil.show(msg);
			}
		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		ButterKnife.unbind(this);
	}
}

